package com.tnl.app.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tnl.app.dao.TbFormDAO;
import com.tnl.app.entity.TbForm;
import com.tnl.base.web.JsonController;

@Controller
@RequestMapping("/form")
public class FormTempController extends JsonController {
	
	@Autowired
	private TbFormDAO tbFormDAO;
	
	/**
	 * 保存推送码
	 * @param id
	 * @param response
	 * [{"formId":"1545204257401","expire":1545809059},{"formId":"1545208689559","expire":1545813491}]
	 */
	@RequestMapping(value = "saveFormIds")
	public void  sendFormIds(String unionid,String openid,String formids,Integer type,HttpServletResponse response){
		System.out.println(formids);
		String formidjson,expirejson;
		TbForm form=new TbForm();
		if(formids!=null){
			JSONArray jsonArray=JSONArray.parseArray(formids);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject=jsonArray.getJSONObject(i);
				formidjson=jsonObject.getString("formId");
				expirejson=jsonObject.getString("expire");
				form.setUnionid(unionid);
				form.setOpenid(openid);
				form.setFormid(formidjson);
				form.setExpire(expirejson);
				form.setWxapptype(type);//职道1,医人才2
				tbFormDAO.save(form);
			}
		}
		this.successJson(formids, "保存推送码", response);
	}
	
	@RequestMapping(value = "selectFormId")
	public void  selectFormId(String formid,Integer type,HttpServletResponse response){
		TbForm fromid=tbFormDAO.getFormidType(formid,type);
		if(fromid!=null){
			this.successJson(fromid, "查询推送码", response);
		}else{
			this.errorJson(0, "查询推送码", response);
		}
		
	}
		
	@RequestMapping(value = "deleteFormId")
	public void  deleteFormId(String formid,HttpServletResponse response){
		TbForm form=tbFormDAO.getFormid(formid);
		if(form.getWxapptype()==1){
			int res=tbFormDAO.delete(formid);
			this.successJson(res, "删除推送码", response);
		}else{
			this.errorJson(0, "未删除", response);
		}
		
	}
	
	
}
